package gptma.bo.dao.employee;

import gptma.bo.dao.FetchInformation;
import java.util.List;

import gptma.bo.dao.GenericDaoJpaImpl;
import gptma.bo.model.employee.Employee;
import gptma.bo.model.project.Project;
import gptma.bo.model.task.Task;

public class EmployeeDaoImpl extends GenericDaoJpaImpl<Employee, Long>
		implements EmployeeDao {

	@Override
	public Employee findEmployee(String trigram) {
		return (Employee) em.createNamedQuery("Employee.byTrigram")
				.setParameter("trigram", trigram).getSingleResult();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Task> findAssignedTasks(Employee employee) {
		return em.createNamedQuery("Employee.findAssignedTasks")
				.setParameter("employee", employee)
				.setHint(HIBERNATE_QUERY_CACHE, true).getResultList();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Task> findAssignedTasks(Employee employee,
			FetchInformation fetchInformation) {
		return em.createNamedQuery("Employee.findAssignedTasks")
				.setParameter("employee", employee)
				.setHint(HIBERNATE_QUERY_CACHE, true)
				.setFirstResult(fetchInformation.getFirst())
				.setMaxResults(fetchInformation.getPageSize()).getResultList();
	}

	@Override
	public long findAssignedTasksCount(Employee employee) {
		return (Long) em.createNamedQuery("Employee.findAssignedTasksCount")
				.setParameter("employee", employee).getSingleResult();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Project> findManagedProjects(Employee employee) {
		return em.createNamedQuery("Employee.findManagedProjects")
				.setParameter("chief", employee).getResultList();
	}

}
